package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.Vector;

import javax.swing.JPanel;

import dal.object.MapObject;
import dal.object.Tool;
import dal.object.tools.*;

@SuppressWarnings("serial")
public class ToolContainer extends JPanel {
	private Vector<Tool> tools;
	private BufferedImage img;
	private Graphics grap;
	private int w, h, x, y;
	
	public ToolContainer(){
		tools = new Vector<Tool>();
		
		this.w = 800;
		this.h = 51;
		this.x = 0;
		this.y = 600 - this.h;
		
		this.setCursor(GameConstants.INVISIBLE_CURSOR);
		setOpaque(false);
		setSize(this.w, this.h);
		setVisible(false);
		repaintAll();
	}
	
	public Tool useTool() {
		if(tools.size() != 0){
			Tool tool = tools.get(0);
			
			if( !(tool instanceof Life) ){
				tools.remove(0);
				repaintAll();
				
				return tool;
			}
		} 
		
		return null;
	}
	
	public void rotate() {
		Tool tool = tools.remove(0);
		tools.add(tool);
		repaintAll();
	}
	
	public void addTool(Tool tool){
		tools.add(tool);
		repaintAll();
	}
	
	public void repaintAll(){
		img = new BufferedImage(this.w, this.h, BufferedImage.TRANSLUCENT);
		grap = img.getGraphics();
		grap.setColor(Color.BLACK);
		grap.fillRect(0, 0, w, h);
		grap.setColor(Color.ORANGE);
		grap.drawRect(3, 3, w - 6, h - 6);
		
		for(int i = 0; i < tools.size(); ++i){
			MapObject mo = tools.get(i);
			mo.setLocation(new Point(GameConstants.picWidth100 * i + 3, 4));
			mo.drawObject(grap);
		}
		
		repaint();
	}
	
	public BufferedImage getImage(){
		return img;
	}
	
	public void paintComponent(Graphics g){
		g.drawImage(img, x, y, null);
	}

	public boolean hasLives() {
		for(int i = 0; i < tools.size(); ++i){
			if(tools.get(i) instanceof Life){
				tools.remove(i);
				repaintAll();
				
				return true;
			}
		}
		
		return false;
	}

	public void setDimension(Dimension size) {
		int w = (int) size.getWidth();
		int h = (int) size.getHeight();
		
		if(w > 0 && h > 0){
			this.w = w;
			this.y = h - this.h;

			setSize(this.w, this.h);
		}
		
		repaintAll();
	}
}
